package cn.com.pc.content.interceptor;

import cn.com.pc.content.domain.Biz;
import cn.com.pc.content.domain.content.ContentType;
import cn.com.pc.content.domain.Site;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
public class HandleHeaderInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        // uri 非法校验
//        String uri = request.getRequestURI();
//        if (uri.contains("//") || uri.contains("%")) {
//            response.setStatus(HttpStatus.SC_NOT_FOUND);
//            response.getWriter().println("Not Found");
//            return false;
//        }

        // options请求直接放行
        String method = request.getMethod();
        if ("options".equalsIgnoreCase(method)) {
            return true;
        }

        String siteValue = request.getHeader("Site");
        String bizValue = request.getHeader("Biz");
        String contentTypeValue = request.getHeader("ContentType");

        if (!StringUtils.hasLength(siteValue)) {
            response.setStatus(HttpStatus.SC_FORBIDDEN);
			response.getWriter().println("Forbidden");
            return false;
        } else {
            try {
                Site site = Site.valueOf(siteValue);
                request.setAttribute("site", site);
            } catch (Exception e) {
                log.warn("site:{} 有误", siteValue);
                response.setStatus(HttpStatus.SC_FORBIDDEN);
                response.getWriter().println("Forbidden");
                return false;
            }
        }

        if (StringUtils.hasLength(bizValue)) {
            try {
                Biz biz = Biz.valueOf(bizValue);
                request.setAttribute("biz", biz);
            } catch (Exception e) {
                log.warn("biz:{} 有误", bizValue);
                response.setStatus(HttpStatus.SC_FORBIDDEN);
                response.getWriter().println("Forbidden");
                return false;
            }
        }

        if (StringUtils.hasLength(contentTypeValue)) {
            try {
                ContentType contentType = ContentType.valueOf(contentTypeValue);
                // Article 和 Video 类型 使用专用接口
                if (ContentType.specialContentTypes.contains(contentType) && request.getRequestURI().contains("customized-content") && !request.getRequestURI().contains("customized-content/detail") && !request.getRequestURI().contains("customized-content/recall")) {
                    response.setStatus(HttpStatus.SC_FORBIDDEN);
                    response.getWriter().println("Forbidden");
                    return false;
                }
                request.setAttribute("contentType", contentType);
            } catch (Exception e) {
                log.warn("contentType:{} 有误", contentTypeValue);
                response.setStatus(HttpStatus.SC_FORBIDDEN);
                response.getWriter().println("Forbidden");
                return false;
            }
        }

        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }

}
